﻿using System;
using System.Windows.Forms;
using CRMS.Models;

namespace CRMS.Views.TeacherMgnt
{
    public partial class frmOpenCourseInfo : Form
    {
        private OpenCourseInfoRep tcRep = new OpenCourseInfoRep();
        private BuildingRep buidingRep = new BuildingRep();
        private string eventPassParamater = String.Empty;
        private string _tName = String.Empty;
        public frmOpenCourseInfo(string teacherID,string tName)
        {
            InitializeComponent();
            eventPassParamater = teacherID;
            _tName = tName;
        }

        private void frmOpenCourseInfo_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            if (eventPassParamater == String.Empty)
            {
                dataGridView1.DataSource = tcRep.getAllOpenCourseInfos();
            }
            else
            {
                dataGridView1.DataSource = tcRep.getOpenCourseInfo(eventPassParamater);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                txtCourseId.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbCourseId.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCourseCategory.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCourseCredit.Text = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbRoomID.Text = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbPeriod.Text = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbWeekday.Text = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                cbTeacherId.Text = dataGridView1[7, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherId.Text = dataGridView1[8, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherSex.Text = dataGridView1[9, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtTeacherTitle.Text = dataGridView1[10, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtDepartmentName.Text = dataGridView1[11, dataGridView1.CurrentCell.RowIndex].Value.ToString();

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            SetDefaultValue();
            
            btnAdd.Text = "保  存";
            btnAdd.Click -= new EventHandler(this.btnAdd_Click);
            btnAdd.Click += new EventHandler(btnAdd_Save);
            btnModify.Enabled = false;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            SetDefaultValue();
            btnModify.Text = "保  存";
            btnModify.Click -= new EventHandler(this.btnModify_Click);
            btnModify.Click += new EventHandler(btnModify_Save);
            btnAdd.Enabled = false;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            TeacherCourse tc = new TeacherCourse();
            tc = tcRep.getOpenCourse(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                dataGridView1[8, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (tc == null)
            {
                MessageBox.Show("该信息不存在或者已经被删除！");
            }
            else
            {
                DialogResult result = MessageBox.Show("您确定删除？","删除提示",MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    tcRep.Delete(tc);
                    tcRep.Save();
                    MessageBox.Show("删除成功！");
                    this.frmOpenCourseInfo_Load(sender, e);
                }
                else
                {
                    return;
                }
            }
        }

        private void btnAdd_Save(object sender, EventArgs e)
        {
            setDefualtStatus();
            TeacherCourse tc = new TeacherCourse();
            ToUseClassroom tuc = new ToUseClassroom();
            if (cbCourseId.Text.Trim() == String.Empty
                || cbTeacherId.Text.Trim() == String.Empty)
            {
                MessageBox.Show("所有字段不能为空！");
            }
            else
            {

                if (tcRep.getOpenCourse(((CRMS.Models.OpenCourseInfoRep.course)cbCourseId.Items[cbCourseId.SelectedIndex]).cID,
                    ((CRMS.Models.OpenCourseInfoRep.teacher)cbTeacherId.Items[cbTeacherId.SelectedIndex]).tID) != null)
                {
                    MessageBox.Show("该信息已经存在！");
                }
                else
                {
                    tc.ClassroomId = ((CRMS.Models.OpenCourseInfoRep.classroom)cbRoomID.Items[cbRoomID.SelectedIndex]).roomID;
                    tc.CourseId = ((CRMS.Models.OpenCourseInfoRep.course)cbCourseId.Items[cbCourseId.SelectedIndex]).cID;
                    tc.TeacherId = ((CRMS.Models.OpenCourseInfoRep.teacher)cbTeacherId.Items[cbTeacherId.SelectedIndex]).tID;
                    tc.Weekday = cbWeekday.Text.Trim();
                    tc.Period = cbPeriod.Text.Trim();
                    //登记教室使用信息
                    tuc.TeachingBuildingId = ((TeachingBuilding)cbTBuilding.Items[cbTBuilding.SelectedIndex]).TeachingBuildingId;
                    tuc.ClassroomId = tc.ClassroomId;
                    tuc.UserId = tc.TeacherId;
                    tuc.UseDate = DateTime.Now;
                    tuc.Weekday = tc.Weekday;
                    tuc.Period = tc.Period;
                    tuc.UseAs = "上" + cbCourseId.Text.Trim() + "课程";
                    tuc.UseStatus = '0';
                    //登记教室使用信息

                    tcRep.Add(tc);
                    tcRep.Add(tuc);//登记教室使用信息
                    tcRep.Save();
                    MessageBox.Show("添加成功！");
                    this.frmOpenCourseInfo_Load(sender, e);
                    this.btnAdd.Text = "添  加";
                    btnAdd.Click -= new EventHandler(btnAdd_Save);
                    btnAdd.Click += new EventHandler(this.btnAdd_Click);
                    btnModify.Enabled = true;
                }
            }

        }

        private void setDefualtStatus()
        {
            cbCourseId.Enabled = false;
            cbPeriod.Enabled = false;
            cbRoomID.Enabled = false;
            cbTBuilding.Enabled = false;
            cbTeacherId.Enabled = false;
            cbWeekday.Enabled = false;
        }

        private void btnModify_Save(object sender, EventArgs e)
        {
            setDefualtStatus();
            TeacherCourse tc = tcRep.getOpenCourse(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                dataGridView1[8, dataGridView1.CurrentCell.RowIndex].Value.ToString());
            if (tc == null)
            {
                MessageBox.Show("该信息不存在！");
            }
            else
            {
                tc.ClassroomId = ((CRMS.Models.OpenCourseInfoRep.classroom)cbRoomID.Items[cbRoomID.SelectedIndex]).roomID;
                tc.CourseId = ((CRMS.Models.OpenCourseInfoRep.course)cbCourseId.Items[cbCourseId.SelectedIndex]).cID;
                tc.TeacherId = ((CRMS.Models.OpenCourseInfoRep.teacher)cbTeacherId.Items[cbTeacherId.SelectedIndex]).tID;
                tc.Weekday = cbWeekday.Text.Trim();
                tc.Period = cbPeriod.Text.Trim();
                tcRep.Save();
                MessageBox.Show("修改成功！");
                this.frmOpenCourseInfo_Load(sender, e);
                btnModify.Text = "修  改";
                btnModify.Click -= new EventHandler(btnModify_Save);
                btnModify.Click += new EventHandler(this.btnModify_Click);
                btnAdd.Enabled = true;
            }
        }

        private void SetDefaultValue()
        {
            cbCourseId.Enabled = true;
            cbCourseId.DataSource = tcRep.getCourses();
            cbCourseId.DisplayMember = "cName";
            cbCourseId.ValueMember = "cID";

            cbTBuilding.Enabled = true;
            cbTBuilding.DataSource = buidingRep.GetBuildings();
            cbTBuilding.DisplayMember = "TeachingBuildingName";
            cbTBuilding.ValueMember = "TeachingBuildingId";

            cbTeacherId.Enabled = true;
            cbTeacherId.DataSource = tcRep.getTeachers();
            cbTeacherId.DisplayMember = "tName";
            cbTeacherId.ValueMember = "tID";
            if (_tName == String.Empty)
            {
                cbTeacherId.SelectedIndex = 0;
            }
            else
            {
                cbTeacherId.SelectedIndex = cbTeacherId.FindString(_tName);
            }
            cbWeekday.Enabled = true;
            cbPeriod.Enabled = true;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = tcRep.getOpenCourseInfoByTName(txtInputTeacherName.Text.Trim());
        }

        private void cbTBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbRoomID.Enabled = true;
            cbRoomID.Text = String.Empty;
            cbRoomID.DataSource = tcRep.getRooms(((TeachingBuilding)cbTBuilding.Items[cbTBuilding.SelectedIndex]).TeachingBuildingId);
            cbRoomID.DisplayMember = "roomID";
            cbRoomID.ValueMember = "roomID";
        }
    }
}
